﻿using System;
using System.Collections.Generic;
using System.Text;
using ComponentFactory.Krypton.Toolkit;
using System.Drawing;
using System.Management;
using System.Net.NetworkInformation;

namespace MeTalker.Aide.Forms
{
    internal class MeForm : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        public MeForm()
        {
            InitializeComponent();
            try
            {
                this.Icon = new Icon("icons/icon.ico");
                this.TextExtra = "www.jinlansoft.com"; //System.Configuration.ConfigurationManager.AppSettings["mainurl"];
            }
            catch
            {
            }
        }

        protected byte[] HexToByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }

        protected string ByteToHexX2(byte[] bytes)
        {
            string returnstr = "";
            if (bytes != null)
                foreach (byte b in bytes)
                    returnstr += b.ToString("X2");

            return returnstr;
        }

        protected string GetHDID()
        {
            try
            {
                //获取硬盘ID  
                String HDid;
                ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc1 = cimobject1.GetInstances();
                foreach (ManagementObject mo in moc1)
                {
                    HDid = (string)mo.Properties["Model"].Value;
                    return HDid.ToString();
                }
                ////获取网卡硬件地址  
                //ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                //ManagementObjectCollection moc2 = mc.GetInstances();
                //foreach (ManagementObject mo in moc2)
                //{
                //    if ((bool)mo["IPEnabled"] == true)
                //        Response.Write("MAC address\t{0}" + mo["MacAddress"].ToString());
                //    mo.Dispose();
                //}  
            }
            catch
            {
            }
            return string.Empty;
        }

        //获取cpu信息 
        protected string GetCpuIndex()
        {
            try
            {
                string _cpuInfo = "";
                ManagementClass cimobject = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = cimobject.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    _cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
                return _cpuInfo;
            }
            catch { }
            return string.Empty;
        }

        // 得到硬盘序列号
        protected string GetHDIndex()
        {
            try
            {
                string _HDInfo = "";
                ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc1 = cimobject1.GetInstances();
                foreach (ManagementObject mo in moc1)
                {
                    _HDInfo = (string)mo.Properties["Model"].Value;


                }
                return _HDInfo;
            }
            catch { } 
            return string.Empty;
        }

        //获取网卡硬件地址
        protected string GetMacAddress()
        {
            try
            {
                string _MacAddress = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc2 = mc.GetInstances();
                foreach (ManagementObject mo in moc2)
                {
                    if ((bool)mo["IPEnabled"] == true)
                        _MacAddress = mo["MacAddress"].ToString();
                    mo.Dispose();
                }

                return _MacAddress;
            }
            catch { }
            return string.Empty;
        }


        protected string GetHd()
        {
            try
            {
                ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher();
                wmiSearcher.Query = new SelectQuery("Win32_DiskDrive", "", new string[] { "PNPDeviceID" }
                 );
                ManagementObjectCollection myCollection = wmiSearcher.Get();
                ManagementObjectCollection.ManagementObjectEnumerator em =
                myCollection.GetEnumerator();
                em.MoveNext();
                ManagementBaseObject mo = em.Current;
                string id = mo.Properties["PNPDeviceID"].Value.ToString().Trim();
                return id;
            }
            catch
            {
                return "";
            }
        }

        //取第一块硬盘序列号
        protected string GetHardDiskID2()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
                String strHardDiskID = null;
                foreach (ManagementObject mo in searcher.Get())
                {
                    strHardDiskID = mo["SerialNumber"].ToString().Trim();
                    break;
                }
                return strHardDiskID;
            }
            catch
            {
                return "";
            }
        }

        protected List<string> GetNetworkInterfaces()
        {
            List<string> macs = new List<string>();
            IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            if (nics == null || nics.Length < 1)
            {
                return macs;
            }
            foreach (NetworkInterface adapter in nics)
            {
                IPInterfaceProperties properties = adapter.GetIPProperties();
                PhysicalAddress address = adapter.GetPhysicalAddress();
                byte[] bytes = address.GetAddressBytes();
                string mac = string.Empty;
                for (int i = 0; i < bytes.Length; i++)
                {
                    mac += bytes[i].ToString("X2");
                    if (i != bytes.Length - 1)
                    {
                        mac += "-";
                    }
                }
                macs.Add(mac);
            }
            return macs;
        }
        protected string GetNetworkInterfaceToString()
        {
            List<string> str = GetNetworkInterfaces();
            StringBuilder sb = new StringBuilder();
            foreach (var s in str) { sb.Append(s + ","); }
            return sb.ToString();
        }
        protected string GetCPUSerialNo()
        {
            string str = "";
            try
            {
                ManagementClass class2 = new ManagementClass("Win32_Processor");
                foreach (ManagementObject obj2 in class2.GetInstances())
                {
                    str = str + obj2.Properties["ProcessorId"].Value.ToString();
                }
                str = str.Replace("-", "");
                str = str.Replace("_", "");
                str = str.Replace("/", "");
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str;
        }

        protected string GetDiskID()
        {
            String HDid = null;
            ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc1 = cimobject1.GetInstances();
            foreach (ManagementObject mo in moc1)
            {
                HDid = mo.Properties["Model"].Value as string;
            }
            return HDid;
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MeForm));
            this.kryptonManager1 = new ComponentFactory.Krypton.Toolkit.KryptonManager(this.components);
            this.SuspendLayout();
            // 
            // kryptonManager1
            // 
            this.kryptonManager1.GlobalPaletteMode = ComponentFactory.Krypton.Toolkit.PaletteModeManager.Office2007Blue;
            // 
            // MeForm
            // 
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "MeForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.ResumeLayout(false);

        }
        private ComponentFactory.Krypton.Toolkit.KryptonManager kryptonManager1;
        private System.ComponentModel.IContainer components;
    }
}